package com.shawn.spring.boot.mybatisplus.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.Arrays;

/**
 * @author shawn
 * @since 2020/8/30
 */
public class MybatisPlusCodeGenerator {

  public static void main(String[] args) {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();

    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath + "/spring/springboot/spring-boot-mybatis-plus/src/main/java");
    gc.setAuthor("shawn");
    gc.setOpen(false);
    gc.setServiceName("%sService");
    gc.setFileOverride(true);
    gc.setIdType(IdType.AUTO);
    gc.setDateType(DateType.ONLY_DATE);
    // gc.setSwagger2(true); 实体属性 Swagger2 注解
    mpg.setGlobalConfig(gc);

    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl(
        "jdbc:mysql://localhost:3306/spring-boot-demo?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("Gepoint");
    dsc.setDbType(DbType.MYSQL);
    mpg.setDataSource(dsc);

    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setParent("com.shawn.spring.boot.mybatisplus");
    pc.setController("controller");
    pc.setService("service");
    pc.setEntity("entity");
    pc.setMapper("mapper");
    mpg.setPackageInfo(pc);

    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setControllerMappingHyphenStyle(true);
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    strategy.setTablePrefix("t_");
    strategy.setLogicDeleteFieldName("is_deleted");
    TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
    TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
    strategy.setTableFillList(Arrays.asList(createTime, updateTime));
    mpg.setStrategy(strategy);
    mpg.execute();
  }
}
